package com.example.demoapp.service;

import cn.hutool.core.util.StrUtil;
import com.example.demoapp.entity.Employee;
import com.example.demoapp.exception.BusinessException;
import com.example.demoapp.mapper.EmployeeMapper;
import com.example.demoapp.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import java.util.List;



@Service
public class EmployeeService {

    //引入employeeMapper资源
    @Resource
    private EmployeeMapper employeeMapper;

    //查询表的所有信息
    public List<Employee> selecAll() {
        //业务层，写业务代码
        return employeeMapper.selecAll();
    }

    //根据id查询信息
    public Employee selecByid(Integer id){
        return employeeMapper.selecByid(id);
    }

    //这里使用了pageInfo这个插件 会自动的去给select * 增加limit来实现分页查询数据,使用 selecAll查询到所有的数据 在用PageHelper过滤出需要的对应页的数据
    public PageInfo<Employee> pageSelect(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Employee> list =  employeeMapper.selecAll();
        return PageInfo.of(list);
    }
    //新增用户
    public int save(Employee employee) {
        return employeeMapper.inSert(employee);
    }

    public int del(Integer id) {
        return employeeMapper.del(id);
    }

    public int updata(Employee employee) {
        return employeeMapper.updata(employee);
    }

    public Employee login(Employee employee) {
        //获取账号密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //查询账号是否存在
        Employee dbEmployee = employeeMapper.slectByusername(username);
        if(dbEmployee ==  null){//没查询到相关账号 抛出错误
            throw new BusinessException(500,"账号不存在");
        }

        //校验密码
        if(!dbEmployee.getPassword().equals(password)){
            throw new BusinessException(500,"账号或者密码错误");
        }

        //设置并且给前端返回token
        String token = TokenUtils.createToken(dbEmployee.getId()+"-"+"user",dbEmployee.getPassword());
        dbEmployee.setToken(token);
        return dbEmployee;
    }

    public int register(Employee employee) {
        //获取账号密码
        String username = employee.getUsername();
        //查看注册的账号是否已经存在
        Employee dbEmployee = employeeMapper.slectByusername(username);
        if(dbEmployee != null){//没查询到相关账号 抛出错误
            throw new BusinessException(500,"该账号已注册，请直接登入");
        }

        //校验名字和密码是否为空,设置初始值
        if(StrUtil.isBlank(employee.getName())){
            employee.setName("iniUser");
        }
        if(StrUtil.isBlank(employee.getPassword())){
            employee.setPassword("123456");
        }

        //给所有角色设置初始角色
        employee.setRole("employs");


        //注册
        return employeeMapper.inSert(employee);
    }

    public void saveFilepath(String downloadPath,Integer id) {
        //校验id是否存在,用id去查 能查出来就说明存在
        Employee employee = employeeMapper.selecByid(id);
        if(employee == null){
            throw new BusinessException(400,"员工id不存在");
        }
        employeeMapper.saveFilepath(downloadPath,id);
    }
}
